/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_ora_pop_ttc7_new_stmt.c                          */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/22 by geyh                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frdb_cursor.h"
#include "frct_comm.h"

#include "../../../../src/module/oracle/config.h"
#include "../../test_ora_comm.h"
#include <apmtest_comm.h>
#include <oracle/comm/ora_stmt.h>



extern void test_ora_destroy_ct(Conntrack *p_st_conn);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}


static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    Sql_statement *p_st_stmt = NULL;
    Sql_statement *p_st_stmt_out = NULL;
    int i_ret = 0;

    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);


    p_st_stmt = (Sql_statement *)frmalloc(sizeof(Sql_statement));
    memset(p_st_stmt, 0x00, sizeof(Sql_statement));

    frdb_add_statement(p_st_session_private->p_hash_stmt, 1, p_st_stmt);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    p_st_stmt_out = ora_get_stmt(p_st_conn, 1);

    CU_ASSERT_PTR_NOT_NULL(p_st_stmt_out);
    

    ora_free_private_stmt(p_st_session_private);
    ora_clear_ttc7_new_stmt(p_st_conn);

    test_ora_destroy_ct(&p_st_conn);
    return;
}

static void __case2(void)
{
    Conntrack *p_st_conn = NULL;
    Sql_statement *p_st_stmt_out = NULL;

    CU_ASSERT_EQUAL(test_ora_create_ct(&p_st_conn, TEST_ORA_SRC_IP, TEST_ORA_DST_IP, TEST_ORA_SRC_PORT, TEST_ORA_DST_PORT), FR_SUCC);


    p_st_stmt_out = ora_get_stmt(p_st_conn, 1);

    CU_ASSERT_PTR_NULL(p_st_stmt_out);

    ora_clear_ttc7_new_stmt(p_st_conn);
    test_ora_destroy_ct(&p_st_conn);
    return;
}


int add_suite_ora_get_stmt(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" ora_get_stmt()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);

    return FR_SUCC;
}
